import sys
import os
sys.path.append(os.path.join(os.path.dirname(__file__), '..'))
from impl.db.database import DatabaseConnection
from impl.db.model.deduplication import BingCrawlerDeduplicationDAO
import os
from datetime import datetime


def db_count_images(db_config: dict, save_folder: str):
    db = DatabaseConnection(db_config, True)
    count_file_path = os.path.join(save_folder, f'num-images-{datetime.now().strftime("%Y.%m.%d-%H.%M.%S-%f")}.txt')
    with open(count_file_path, 'w', newline='', encoding='utf-8') as fid, db:
        deduplication_dao = BingCrawlerDeduplicationDAO(db)
        count = deduplication_dao.count_all()
        print(count)
        fid.write(str(count))


import argparse


if __name__ == '__main__':
    parser = argparse.ArgumentParser('Count number of images in database')
    parser.add_argument('save_folder', type=str, help='Folder path to store result')
    args = parser.parse_args()

    from db_utils._get_db_config import get_db_config
    db_count_images(get_db_config(), args.save_folder)
